package main

import (
	"bytes"
	"fmt"
	_ "io"
	"io/ioutil"
	"log"
	"net/http"
	"net/url"
)

func main() {
	file := "D:/GoSpace/src/golang-step/day33/192.168.1.51_01_20200115093612619_FACE_SNAP.jpg"
	url := "http://10.88.34.180:80//km/v1/pic/receive"
	postFile(url, file)
}

func listFiles() {
	pwd := ""
	//获取文件或目录相关信息
	fileInfoList, err := ioutil.ReadDir(pwd)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(len(fileInfoList))
	for i := range fileInfoList {
		fmt.Println(fileInfoList[i].Name()) //打印当前文件或目录下的文件或目录名
	}

}

func postFile(url, filename string) {
	//这是一个Post 参数会被返回的地址
	byte, err := ioutil.ReadFile(filename)
	resopne, err := http.Post(url, "multipart/form-data", bytes.NewReader(byte))
	if err != nil {
		fmt.Println("err=", err)
	}
	defer func() {
		resopne.Body.Close()
		fmt.Println("finish")
	}()
	body, err := ioutil.ReadAll(resopne.Body)
	if err != nil {
		fmt.Println(" post err=", err)
	}
	fmt.Println(string(body))
}

func post() {
	//这是一个Post 参数会被返回的地址
	strinUrl := "http://localhost:8080/aaa"
	resopne, err := http.PostForm(strinUrl, url.Values{"num": {"456"}, "num1": {"123"}})

	if err != nil {
		fmt.Println("err=", err)
	}
	defer func() {
		resopne.Body.Close()
		fmt.Println("finish")
	}()

	body, err := ioutil.ReadAll(resopne.Body)
	if err != nil {
		fmt.Println(" post err=", err)
	}
	fmt.Println(string(body))
}
